// mem.hpp
// Memory management for ripescript. You should not use any other functions but
// these.
//
// Copyright (C) 2008  Maksim Sipos <msipos@mailc.net>
//
// This file is a part of the Ripe language project.
//
// Ripe is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#ifndef R_MEM_HPP
#define R_MEM_HPP

#include "standard.hpp"
#include "util/exception.hpp"
#include <cstdlib>
#include <cstring>
#include <new>

namespace Ripe {
  // Thrown when a memory allocation fails.
  class ErrorAlloc : public Exception {};

  // mem_* functions are the fundamental raw memory handling functions. Use
  // these unless as references in Values (in that case, you must use gc_* 
  // functions).

  // Allocates a certain number of bytes on the stack. It is implied that
  // these bytes will be deallocated after the function returns.
  #define mem_alloca(sz) alloca(sz)

  // Frees a memory block. Wrapper around the traditional free().
  void mem_free(void* ptr);

  // Wrapper around the traditional malloc().
  void* mem_malloc(size_t size) throw (ErrorAlloc);

  // Wrapper around the traditional realloc().
  void* mem_realloc(void* ptr, size_t size) throw (ErrorAlloc);

  // Duplicates a C-style string.
  char* mem_strdup(const char* s) throw(ErrorAlloc);
}

// Globally overloaded operators:

// Overloaded new operators that implement regular malloc.
void *operator new(size_t size) throw (Ripe::ErrorAlloc);
void *operator new[](size_t size) throw (Ripe::ErrorAlloc);
void operator delete(void *p);
void operator delete[](void *p);

#endif // R_MEM_HPP
